/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.core.grid;

import com.blandware.atleap.common.util.ConvertUtil;
import com.blandware.atleap.webapp.form.core.StringFilterForm;
import com.blandware.atleap.webapp.taglib.core.grid.util.FilterConditions;
import com.blandware.atleap.webapp.taglib.core.grid.util.Grid;
import com.blandware.atleap.webapp.taglib.core.grid.util.StringFilter;
import com.blandware.atleap.webapp.taglib.core.util.TaglibConstants;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <p>Action for string filter
 * </p>
 * <p><a href="StringFilterAction.java.html"><i>View Source</i></a></p>
 * <p/>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.7 $ $Date: 2008/05/23 08:09:16 $
 * @struts.action path="/grid/stringFilter"
 * input="showFilterPage"
 * scope="request"
 * validate="true"
 * name="stringFilterForm"
 * @struts.action-forward name="showFilterPage"
 * path=".stringFilter"
 * @struts.action-forward name="filterError"
 * path=".filterError"
 */
public final class StringFilterAction extends com.blandware.atleap.webapp.action.core.grid.BaseGridAction {

	/**
	 * @param mapping  The ActionMapping used to select this instance
	 * @param form     The optional ActionForm bean for this request (if any)
	 * @param request  The HTTP request we are proceeding
	 * @param response The HTTP response we are creating
	 * @return an ActionForward instance describing where and how
	 *         control should be forwarded, or null if response
	 *         has already been completed
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	                             HttpServletRequest request, HttpServletResponse response) {

		HttpSession session = request.getSession();
		StringFilterForm stringFilterForm = (StringFilterForm) form;

		String fieldName = stringFilterForm.getFieldName();
		String rowIterators = stringFilterForm.getRowIterators();
		String gridName = stringFilterForm.getGridName();
		Grid grid = getGridByName(gridName, session);

		if ( grid == null ) {
			if ( log.isWarnEnabled() ) {
				String errorMessage = "No grid with name " + gridName + " could be found in session";
				log.warn(errorMessage);
			}
			return mapping.findForward("filterError");
		}

		StringFilter filter = new StringFilter(fieldName);
		filter.setRowIterators(ConvertUtil.convertStringToList(rowIterators, ",", true));

		String firstCondition = stringFilterForm.getFirstCondition();
		String firstValue = stringFilterForm.getFirstValue();
		String secondCondition = stringFilterForm.getSecondCondition();
		String secondValue = stringFilterForm.getSecondValue();
		String logicCopula = stringFilterForm.getLogicCopula();

		// Remove filter if 'clear' button was pressed else create clauses
		// according to specified values

		if ( stringFilterForm.getClearFilter() != null ) {
			grid.removeFilter(filter);
			if ( log.isDebugEnabled() ) {
				log.debug("Filter for field '" + fieldName + "' was removed from grid");
			}
		} else {
			if ( firstCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NULL) || firstCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NOT_NULL) ) {
				filter.createFirstClause(firstCondition, null);
			} else if ( firstValue != null || firstValue.length() != 0 ) {
				filter.createFirstClause(firstCondition, firstValue);
			} else {
				if ( secondCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NULL) || secondCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NOT_NULL) ) {
					filter.createFirstClause(secondCondition, null);
				} else if ( secondValue != null || secondValue.length() != 0 ) {
					filter.createFirstClause(secondCondition, secondValue);
				}
			}

			if ( !secondCondition.equalsIgnoreCase(FilterConditions.CONDITION_NOT_SET) ) {
				if ( secondCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NULL) || secondCondition.equalsIgnoreCase(FilterConditions.OBJECT_IS_NOT_NULL) ) {
					filter.createSecondClause(secondCondition, null);
				} else if ( secondValue != null && secondValue.length() != 0 ) {
					filter.createSecondClause(secondCondition, secondValue, logicCopula);
				}
			}
			grid.addFilter(filter);
			if ( log.isDebugEnabled() ) {
				log.debug("New StringFilter added to grid '" + gridName + "' for field '" + fieldName + "'. Clause is: " + filter.getClause());
			}

		}

		saveGrid(grid, session);
		request.setAttribute(TaglibConstants.PAGE_URL, stringFilterForm.getPageUrl());
		return mapping.findForward("filterReturn");
	}
}